import requests

import urllib.request


class HttpRequest:

    @staticmethod
    def request(method="GET", url=None, header=None, data=None):
        method = method.upper()
        if method == "GET":
            response = urllib.request.urlopen(url)
            content = response.read().decode()
            return content
        elif method == "POST":
            pass


class HttpRequestHelper:

    @classmethod
    def request(cls, method="GET", url=None, data=None, headers=None):  # 这里是要传入的参数，请求方法、接口地址、传参、头文件

        method = method.upper()
        if method == 'POST':
            return requests.post(url=url, data=data, headers=headers)
        elif method == 'GET':
            return requests.get(url=url, params=data, headers=headers)
        elif method == "PUT":
            return requests.put(url=url, data=data)

# class HttpSession(object):
#     """记录Session的方法"""
#     def __init__(self):
#         self.session = requests.session()  # 初始化一个保存session的方法
#
#     def request(self, method, url, data=None, headers=None):
#         method = method.upper()
#         if method == 'POST':
#             return self.session.post(url=url, data=data, headers=headers)
#         elif method == 'GET':
#             return self.session.get(url=url, params=data, headers=headers)
#
#     def close(self):
#         """断开session连接的方法"""
#         self.session.close()


if __name__ == '__main__':
    import json
    url = "http://127.0.0.1:5000/api/MotorDrivenRollerIOTDevice"
    http_request = HttpRequest.request(url=url)
    http_request = json.loads(http_request)
    print(http_request)

